// public/js/script.js
// 标签切换功能
document.querySelectorAll('.tab-btn').forEach(button => {
  button.addEventListener('click', () => {
    document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
    document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
    
    button.classList.add('active');
    document.getElementById(button.dataset.tab + '-tab').classList.add('active');
  });
});

// 从URL生成PDF
async function generateFromURL() {
  const url = document.getElementById('url-input').value;
  if (!url) {
    alert('请输入URL');
    return;
  }

  try {
    const response = await fetch('/api/pdf/generate', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ 
        url, 
        filename: getFilenameFromUrl(url) + '.pdf' 
      })
    });

    if (response.ok) {
      const blob = await response.blob();
      downloadPDF(blob, getFilenameFromUrl(url) + '.pdf');
    } else {
      const error = await response.json();
      alert('生成失败: ' + error.error);
    }
  } catch (error) {
    alert('请求失败: ' + error.message);
  }
}

// 从URL预览PDF
async function previewFromURL() {
  const url = document.getElementById('url-input').value;
  if (!url) return alert('请输入URL');

  try {
    const response = await fetch('/api/pdf/preview', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ url })
    });

    if (response.ok) {
      const blob = await response.blob();
      const url = URL.createObjectURL(blob);
      window.open(url, '_blank');
    } else {
      const error = await response.json();
      alert('预览失败: ' + error.error);
    }
  } catch (error) {
    alert('请求失败: ' + error.message);
  }
}

// 获取网页截图
async function screenshotURL() {
  const url = document.getElementById('url-input').value;
  if (!url) return alert('请输入URL');

  try {
    const response = await fetch('/api/pdf/screenshot', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ url })
    });

    if (response.ok) {
      const blob = await response.blob();
      const imgUrl = URL.createObjectURL(blob);
      const preview = document.getElementById('url-preview');
      preview.innerHTML = `<img src="${imgUrl}" style="max-width: 100%; border: 1px solid #ddd;">`;
    } else {
      const error = await response.json();
      alert('截图失败: ' + error.error);
    }
  } catch (error) {
    alert('请求失败: ' + error.message);
  }
}

// 获取页面信息
async function getPageInfo() {
  const url = document.getElementById('url-input').value;
  if (!url) return alert('请输入URL');

  try {
    const response = await fetch('/api/pdf/page-info', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ url })
    });

    if (response.ok) {
      const info = await response.json();
      alert(`页面标题: ${info.title}\nURL: ${info.url}\n内容长度: ${info.contentLength} 字符`);
    } else {
      const error = await response.json();
      alert('获取信息失败: ' + error.error);
    }
  } catch (error) {
    alert('请求失败: ' + error.message);
  }
}

// 工具函数：从URL提取文件名
function getFilenameFromUrl(url) {
  try {
    const urlObj = new URL(url);
    return urlObj.hostname.replace(/^www\./, '') || 'document';
  } catch {
    return 'document';
  }
}

// 工具函数：下载PDF
function downloadPDF(blob, filename) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}
